package com.lenovo.newsapp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lenovo.newsapp.R;
import com.lenovo.newsapp.util.PreferenceUtil;


/**
 *switch
 * 
 * 
 */
public class SwitchPush extends LinearLayout {
	
	public SwitchPush(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	private ImageView maskImage;              // 圆
	private boolean open =PreferenceUtil.getString("push","").equals("open")||PreferenceUtil.getString("push","").equals("");
	private boolean isAninFinish = true;      //动画是否结束
	private float x;                          // 记录ACTION_DOWN时候的横坐标
	private boolean isChangedByTouch = false; //是否在一次事件中已经切换过状态
	private OnSwitchChangeListener01 switchChangeListener; // 监控开关状态

	public SwitchPush(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	public interface OnSwitchChangeListener01 {
		void onSwitchChanged(boolean open);
	}

	private void init() {
		
		setBackgroundResource(R.drawable.switch01_03);
		
		setSwitchStatus(open);
		
        maskImage = new ImageView(getContext());
		maskImage.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		maskImage.setImageResource(R.drawable.circle_03);
		addView(maskImage);
	}
	
	public boolean getSwitchStatus() {
		return open;
	}
	public void setSwitchStatus(boolean isOpen) {
		this.open = isOpen;
		if (isOpen) {
			setGravity(Gravity.RIGHT);
//			setBackgroundResource(R.drawable.switch01_03);
		} else {
			setGravity(Gravity.LEFT);
//			setBackgroundResource(R.drawable.switch01_04);
		}
	}
	public boolean getstatus(){
		return open;
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			x = event.getX();
			break;
		}
		case MotionEvent.ACTION_MOVE: {
			if (event.getX() - x > 5 && !open) { // 向右
				changeStatus();
			} else if (event.getX() - x < -5 && open) { // 向左
				changeStatus();
			}
			break;
		}
		case MotionEvent.ACTION_UP: {
			if (Math.abs(event.getX() - x) <= 5) {
				changeStatus();
			}
			isChangedByTouch = false;
			break;
		}
		case MotionEvent.ACTION_CANCEL: {
			isChangedByTouch = false;
			break;
			}
		}
		return true;
	}
	private void changeStatus() {
		if (isAninFinish && !isChangedByTouch) {
			isChangedByTouch = true;
			open = !open;
			isAninFinish = false;
			if (switchChangeListener != null) {
				switchChangeListener.onSwitchChanged(open);
			}
			changeOpenStatusWithAnim(open);
		}
	}

	private void changeOpenStatusWithAnim(boolean open) {
		if (open) {
			// 左到右
			Animation leftToRight = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, 
					Animation.ABSOLUTE, getWidth() - maskImage.getWidth(), 
					Animation.RELATIVE_TO_SELF, 0, 
					Animation.RELATIVE_TO_SELF, 0);
			leftToRight.setDuration(200);
			leftToRight.setAnimationListener(new AnimationListener() {
				public void onAnimationStart(Animation animation) {
				}

				public void onAnimationRepeat(Animation animation) {
				}

				public void onAnimationEnd(Animation animation) {
					maskImage.clearAnimation();
					setGravity(Gravity.RIGHT);
					isAninFinish = true;
				}
			});
			//setBackgroundResource(R.drawable.switch0101_06);
			maskImage.startAnimation(leftToRight);
			//PreferenceUtil.commitString("language", "en");
		} else{
			//右到左
			Animation rightToLeft = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
					Animation.ABSOLUTE, maskImage.getWidth() - getWidth(), 
					Animation.RELATIVE_TO_SELF, 0, 
					Animation.RELATIVE_TO_SELF, 0);
			rightToLeft.setDuration(200);
			rightToLeft.setAnimationListener(new AnimationListener() {
				@Override
				public void onAnimationStart(Animation animation) {
				}
				@Override
				public void onAnimationRepeat(Animation animation) {
				}
				@Override
				public void onAnimationEnd(Animation animation) {
					maskImage.clearAnimation();
					setGravity(Gravity.LEFT);
					isAninFinish = true;
				}
			});
			maskImage.startAnimation(rightToLeft);
			//PreferenceUtil.commitString("language", "ch");
		}
	}

	public OnSwitchChangeListener01 getSwitchChangeListener() {
		return switchChangeListener;
	}

	public void setOnSwitchChangeListener(OnSwitchChangeListener01 switchChangeListener) {
		this.switchChangeListener = switchChangeListener;
	}

}
